package com.eclipse.training.swt.helloworld;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;

import com.eclipse.training.swt.helloworld.listeners.MyShellListener;
import com.eclipse.training.swt.helloworld.ui.ClientArea;

public class HelloWorld {

	private static final String HELLO_WORLD = Messages
			.getString("HelloWorld.0"); //$NON-NLS-1$
	private static ClientArea clientArea;
	private static Label statusLine;

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Display display = new Display();
		Shell shell = new Shell(display);

		shell.setLayout(new GridLayout());
		shell.setSize(900, 600);

		ImageRegistry.getInstance().initializeImageRegistry();

		/*
		 * Add a listener in order to handle close event
		 */
		shell.addShellListener(new MyShellListener());

		shell.setText(HELLO_WORLD);
		shell.setImage(ImageRegistry.getInstance().getImage(
				ImageRegistry.IMAGE_SHELL));

		/*
		 * Create menu bar and toolbar
		 */
		createMenuBar(shell);
		createToolBar(shell);

		/*
		 * Create the client area
		 */
		clientArea = new ClientArea(shell, SWT.NONE);

		statusLine = new Label(shell, SWT.BORDER);
		statusLine.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

		shell.open();

		while (!shell.isDisposed()) {
			/*
			 * Reads an event from the operating system's event queue,
			 * dispatches it appropriately, and returns true if there is
			 * potentially more work to do, or false if the caller can sleep
			 * until another event is placed on the event queue.
			 */
			if (!display.readAndDispatch()) {
				/*
				 * If no more entries in the event queue
				 */
				display.sleep();
			}
		}

		ImageRegistry.getInstance().dispose();
		display.dispose();
	}

	public static Label getStatusLine() {
		return statusLine;
	}

	private static void createToolBar(final Shell shell) {
		/*
		 * Create the tool bar
		 */
		ToolBar bar = new ToolBar(shell, SWT.FLAT);

		/*
		 * Add items to the tool bar
		 */
		ToolItem item = new ToolItem(bar, SWT.CHECK);
		item.setImage(Display.getDefault().getSystemImage(SWT.ICON_WORKING));
		item.setToolTipText(Messages.getString("HelloWorld.1")); //$NON-NLS-1$
		item.setSelection(true);
		item.addListener(SWT.Selection, new Listener() {

			@Override
			public void handleEvent(Event event) {
				ToolItem item = (ToolItem) event.widget;

				clientArea.enableTabs(item.getSelection());
			}
		});

		item = new ToolItem(bar, SWT.PUSH);
		item.setImage(Display.getDefault().getSystemImage(SWT.ICON_ERROR));
		item.setToolTipText(Messages.getString("HelloWorld.2")); //$NON-NLS-1$
		item.addListener(SWT.Selection, new Listener() {

			@Override
			public void handleEvent(Event event) {
				shell.close();
			}
		});

		bar.pack();
	}

	private static void createMenuBar(final Shell shell) {
		/*
		 * Create the bar menu
		 */
		Menu menuBar = new Menu(shell, SWT.BAR);

		/*
		 * Add items to the menu bar
		 */
		Menu fileMenu = new Menu(menuBar);

		MenuItem fileItem = new MenuItem(menuBar, SWT.CASCADE);
		fileItem.setText(Messages.getString("HelloWorld.3")); //$NON-NLS-1$
		fileItem.setMenu(fileMenu);

		/*
		 * Create all the items in the File dropdown menu
		 */
		MenuItem openItem = new MenuItem(fileMenu, SWT.NONE);
		openItem.setText(Messages.getString("HelloWorld.4")); //$NON-NLS-1$
		openItem.setImage(ImageRegistry.getInstance().getImage(
				ImageRegistry.IMAGE_MENU_OPEN));

		openItem.addListener(SWT.Selection, new Listener() {

			@Override
			public void handleEvent(Event arg0) {
				/*
				 * Open the file dialog
				 */
				FileDialog fd = new FileDialog(shell, SWT.OPEN);
				fd.setText(Messages.getString("HelloWorld.5")); //$NON-NLS-1$
				fd.setFilterPath("C:/"); //$NON-NLS-1$
				String[] filterExt = { "*.txt", "*.doc", ".rtf", "*.*" }; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
				fd.setFilterExtensions(filterExt);
				String selected = fd.open();

				if (selected != null)
					shell.setText(selected);
			}
		});

		/*
		 * Add a separator
		 */
		new MenuItem(fileMenu, SWT.SEPARATOR);

		MenuItem exitItem = new MenuItem(fileMenu, SWT.NONE);
		exitItem.setText(Messages.getString("HelloWorld.11")); //$NON-NLS-1$
		exitItem.setImage(ImageRegistry.getInstance().getImage(
				ImageRegistry.IMAGE_MENU_EXIT));
		exitItem.addListener(SWT.Selection, new Listener() {

			@Override
			public void handleEvent(Event arg0) {
				shell.close();
			}
		});

		shell.setMenuBar(menuBar);
	}

}
